package com.xml.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.xml.domain.Book;
import com.xml.utils.HibernateUtils;

public class BookDaoImpl implements BookDao {

	@Override
	public Book insertBook(Book book) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		session.save(book);
		transaction.commit();
		session.close();
		return book;
	}

	@Override
	public boolean deleteBook(int id) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			Book book = (Book) session.load(Book.class, id);
			session.delete(book);
			session.getTransaction().commit();
		}catch (Exception e) {
			if(transaction!=null){
				transaction.rollback();
				return false;}
		}finally{
			session.close();
		}
		return true;
	}

	@Override
	public boolean updateBook(Book book) {
		System.out.println(book);
		Session session = HibernateUtils.getSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();
			Book book1 = (Book) session.load(Book.class, book.getId());
			book1.setBookName(book.getBookName());
			book1.setAuthor(book.getAuthor());
			book1.setPrice(book.getPrice());
			session.update(book1);
			transaction.commit();
		}catch (Exception e) {
			if(transaction!=null){
				transaction.rollback();
				return false;}
		}finally{
			session.close();
		}
		
		return true;
	}

	@Override
	public List<Book> searchBook() {
		// TODO Auto-generated method stub
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		
		List<Book> result = session.createQuery("from Book").list();
		
		transaction.commit();
		session.close();
		
		return result;
	}

	@Override
	public List<Book> pagingQuery(int pageNo, int pageSize) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		
		Query query = session.createQuery("from Book");
		query.setFirstResult(pageNo);
		query.setMaxResults(pageSize);
		List<Book> list = query.list();
		
		transaction.commit();
		session.close();
		
		return list;
	}

	@Override
	public Book query_byId(int id) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		Book book = (Book) session.get(Book.class,id);
		transaction.commit();
		session.close();
		return book;
	}

	@Override
	public boolean checkBook(String id) {
		try{
			Session session = HibernateUtils.getSession();
			Transaction transaction = session.beginTransaction();
			Query query = session.createQuery("from Book where isbn = ?");
			query.setParameter(0,id);
			Book book = (Book) query.list().get(0);
			transaction.commit();
			session.close();
		}catch (Exception e) {
			return false;
		}
		return true;
	}
	
	public List<Book> queryBook(String bookName,String state) {
		List<Book> list = null;
		String hql = null;
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		
		if(bookName.length()>0&&state.equals("a")){
			hql = "from Book b where b.bookName = ?";
			Query query = session.createQuery(hql);
			query.setParameter(0,bookName);
			list = query.list();
		}
		if(bookName.length()>0&&state.equals("b")){
			hql = "from Book b where b.bookName = ? and b.state = ?";
			Query query = session.createQuery(hql);
			query.setParameter(0,bookName);
			query.setParameter(1,0);
			list = query.list();
		}
		if(bookName.length()>0&&state.equals("c")){
			hql = "from Book b where b.bookName = ? and b.state = ?";
			Query query = session.createQuery(hql);
			query.setParameter(0,bookName);
			query.setParameter(1,1);
			list = query.list();
		}
		if(bookName.length()==0&&state.equals("a")){
			hql = "from Book";
			Query query = session.createQuery(hql);
			list = query.list();
		}
		if(bookName.length()==0&&state.equals("b")){
			hql = "from Book b where b.state = ?";
			Query query = session.createQuery(hql);
			query.setParameter(0,0);
			list = query.list();
		}
		if(bookName.length()==0&&state.equals("c")){
			hql = "from Book b where b.state = ?";
			Query query = session.createQuery(hql);
			query.setParameter(0,1);
			list = query.list();
		}
		transaction.commit();
		session.close();
		return list;
	}
	

}
